package com.xxchat.controller;

import com.xxchat.annotation.GlobalInterceptor;
import com.xxchat.entity.constants.UserInfoConstants;
import com.xxchat.entity.dto.TokenUserInfoDTO;
import com.xxchat.entity.enums.ResponseCodeEnum;
import com.xxchat.entity.po.UserInfo;
import com.xxchat.entity.vo.ResponseVO;
import com.xxchat.entity.vo.UserInfoVO;
import com.xxchat.exception.BusinessException;
import com.xxchat.service.UserInfoService;
import com.xxchat.utils.CopyUtils;
import com.xxchat.utils.StringTools;
import com.xxchat.websocket.utils.ChannelContextUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 * @author Waicc
 */
@RestController("userInfoController")
@RequestMapping("/userInfo")
@Validated
public class UserInfoController extends ABaseController {
    @Resource
    private UserInfoService userInfoService;

    @Resource
    private ChannelContextUtils channelContextUtils;

    @RequestMapping("/getUserInfo")
    @GlobalInterceptor
    public ResponseVO getUserInfo(HttpServletRequest request) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        UserInfo userInfo = this.userInfoService.getUserInfoByUserId(userInfoDTO.getUserId());
        UserInfoVO userInfoVO = CopyUtils.copy(userInfo, UserInfoVO.class);
        userInfoVO.setAdmin(userInfoDTO.getAdmin());
        return getSuccessResponseVO(userInfoVO);
    }

    @RequestMapping("/saveUserInfo")
    @GlobalInterceptor
    public ResponseVO saveUserInfo(HttpServletRequest request,
                                   @RequestPart UserInfo userInfo,
                                   @RequestPart MultipartFile avatarFile,
                                   @RequestPart MultipartFile avatarCover) {
        // 1. 校验 nickName 是否为空
        if (StringTools.isEmpty(userInfo.getNickName())) {
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        userInfo.setUserId(userInfoDTO.getUserId());
        userInfo.setPassword(null);
        userInfo.setStatus(null);
        userInfo.setEmail(null);
        userInfo.setCreateTime(null);
        userInfo.setIsDelete(null);
        userInfo.setLastLoginIp(null);
        userInfo.setLastLoginArea(null);
        userInfo.setLastLoginTime(null);
        userInfo.setCurrentLoginIp(null);
        userInfo.setCurrentLoginArea(null);
        userInfo.setCurrentLoginTime(null);

        this.userInfoService.updateUserInfo(userInfo, avatarFile, avatarCover);

        return this.getUserInfo(request);
    }

    @RequestMapping("/updatePassword")
    @GlobalInterceptor
    public ResponseVO updatePassword(HttpServletRequest request,
                                     @NotEmpty @Pattern(regexp = UserInfoConstants.REGEX_PASSWORD)
                                     @Size(max = 32, message = "密码长度不得超过32个字符") String password) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(StringTools.encodeMd5(password));
        this.userInfoService.updateUserInfoByUserId(userInfo, userInfoDTO.getUserId());
        // 强制退出，重新登录
        channelContextUtils.closeContext(userInfoDTO.getUserId());
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/logout")
    @GlobalInterceptor
    public ResponseVO logout(HttpServletRequest request) {
        TokenUserInfoDTO userInfoDTO = getTokenUserInfoDTO(request);
        // 关闭 ws 连接
        channelContextUtils.closeContext(userInfoDTO.getUserId());
        return getSuccessResponseVO(null);
    }
}
